package ge.scenes;

 


import java.util.Collections;

import ge.modules.drawables.Animation;
import ge.modules.drawables.SpriteModule;


public class AnimationTrack extends Track
{

	private SpriteModule sprite;
	/**
	 * That tracks can change the animation of a sprite
	 * in it's temporal line.
	 * @param sprite
	 */
	public AnimationTrack(SpriteModule sprite)
	{
		this.sprite=sprite;
	}
	/**
	 * 	The animation will be activate in the sprite when
	 * 	that frame is reached.
	 * @param time Point in the line time
	 * @param animation Animation of the sprite. If it's null
	 * 			it will be ignored
	 */
	public void addFrame(double time, Animation animation)
	{
		if(animation != null)
		{
			clueFrames.add(new AnimationFrame(time, animation));
			Collections.sort(clueFrames);
		}
	}
	@Override
	protected void reachFrame(Frame f) 
	{	
		AnimationFrame af = (AnimationFrame)f;
		sprite.setAnimation(af.getAnimation());
	}
	 

	@Override
	protected void reachReverseFrame(Frame f) 
	{
		
		System.out.println("Frame "+clueFrames.indexOf(f)+" retrocedido");
		if(actualFrame > 0)
		{
			AnimationFrame af = (AnimationFrame)clueFrames.get(actualFrame-1);
			sprite.setAnimation(af.getAnimation());
		}
		
	}

	@Override
	protected void currentTimeJump(double time, boolean advance) 
	{
		actualFrame = getIndexOfFrameBefore(time);
		if(advance)
		{
			 if(actualFrame > -1)
				 reachFrame(clueFrames.get(actualFrame));
		}
		else
		{
			//Si vamos aun no hemos llegado al Frame, estamos en el siguiente
			if(actualFrame > -1)
				 reachReverseFrame(clueFrames.get(actualFrame-1));
			
		}
		
	}

	private class AnimationFrame extends Frame
	{
		private Animation animation;
		/**
		 * 	
		 * @param time time of the frame
		 * @param animation Animation to be activate
		 */
		public AnimationFrame(double time, Animation animation)
		{
			super(time);
			this.animation = animation;
		}
		public Animation getAnimation() {
			return animation;
		}
		
	}
	
}
